import { invoke } from '@tauri-apps/api/core'

export async function executeCommand(
  command: string,
  args?: string[],
  cwd?: string,
  encoding: string = 'gbk'
): Promise<string> {
  if (!cwd) {
    console.log(`executeCommand: ${command} ${args?.join(' ') || ''}`)
  } else {
    console.log(`cwd: ${cwd}\nexecuteCommand: ${command} ${args?.join(' ') || ''}`)
  }
  const result = (await invoke('execute_command', {
    command: command,
    args: args ?? [],
    cwd: cwd,
    encoding: encoding
  })) as string
  console.log(`executeCommand result: ${result}`)
  return result
}
